package xyz.hubery.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.DateUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import xyz.hubery.service.FileService;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

@DubboService
public class FileServiceImpl implements FileService {

    @Autowired
    private OSS ossClient;
    @Value("${oss.bucket.name:hubery-coin-exchange-images}")
    private String bucketName;
    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endPoint;
    @Value("${spring.cloud.alicloud.access-key}")

    /**
     * 上传图片
     *
     * @param fileBytes 文件字节数组
     * @param fileName  文件名
     * @return 文件访问路径
     */
    @Override
    public String uploadImage(byte[] fileBytes, String fileName) {
        // 保存文件到OSS
        ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(fileBytes));
        // 返回文件的路径
        return "https://" + bucketName + "." + endPoint + "/" + fileName;
    }

    /**
     * 删除图片
     *
     * @param filePath 文件访问路径
     */
    @Override
    public void delete(String filePath) {
        // 删除图片
        ossClient.deleteObject(bucketName, filePath.substring(filePath.lastIndexOf("/") + 1));
    }
}
